
package com.heimali.happyreader.utils;

import org.apache.http.client.utils.URIUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;

public class HttpUtils {
    public static String buildQuery(Map<String, String> map) {
        String res = "";
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
            res += entry.getKey() + "=" + entry.getValue() + "&";
        }
        return res;
    }

    public static String buildQuery(String[]... args) {
        String res = "";
        for (String[] item : args) {
            res += item[0] + "=" + item[1] + "&";
        }
        return res.substring(0, res.length() - 1);
    }

    public static URI buildURI(String protocol, String host, int port, String path, String query) {
        try {
            return URIUtils.createURI(protocol, host, port, path, query, null);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }
}
